/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.example.demo.web;

import java.util.List;

import com.example.demo.entity.SysUser;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author danny
 */
@Controller
public class HelloController {
    
    @Autowired
    UserService userService;
    
    @RequestMapping("hello")
    @ResponseBody
    public String hello() {
        return "Hello Danny";
    }
    
    @RequestMapping("user")
    @ResponseBody
    public User User() {
        User user = new User();
        user.setAge(6);
        user.setName("王老五");
        return user;
    }
    
    @RequestMapping("hello-world")
    public String helloWorld(ModelMap model) {
        model.addAttribute("user", userService.getUserById(2));
        return "index";
    }
    
    @RequestMapping("userList")
    public String userList() {
        return "userList";
    }
    
    @RequestMapping("userData")
    @ResponseBody
    public ModelMap userData() {
        ModelMap rm = new ModelMap();
        
        List<SysUser> rl = userService.getAllSysUser();
        Page<SysUser> pi = (Page<SysUser>) rl;
        rm.addAttribute("rows", rl).addAttribute("total", pi.getTotal());
        return rm;
    }
}
